package com.hyb.algorithm.data.struct.dp;

/**
 * @author: ybhu
 * @create: 2021/08/24 00:14
 *
 * https://leetcode-cn.com/problems/longest-palindromic-subsequence/
 *
 * 516. 最长回文子序列
 */
public class LongestPalindromeSubseq {


    public int longestPalindromeSubseq(String s) {

        if(s==null||s.length()<=0){
            return 0;
        }



        int n=s.length();
        char[] charArray=s.toCharArray();

        int[][] dp=new int[n+1][n+1];

        for(int i=0;i<n;i++){
            dp[i][i]=1;
        }
        //反着遍历
        for(int i=n-1;i>=0;i--){
            for(int j=i+1;j<n;j++){
                if(charArray[i]==charArray[j]){
                    dp[i][j]=dp[i+1][j-1]+2;
                }else{
                    dp[i][j]=Math.max(dp[i+1][j],dp[i][j-1]);
                }
            }
        }

        //右上角 就是 s[i][j] 的值
        return  dp[0][n-1];

    }
}
